var SystemModel = require('../model/system_model.js')

function setValue(key, value) {
  var enKey = key
  var enValue = value
  wx.setStorage({
    key: enKey,
    data: enValue,
  })
}

function value(key) {
  var enKey = key
  var enValue = wx.getStorageSync(enKey)
  var deValue = enValue
  return deValue
}

function setUid(uid) {
  wx.setStorageSync('uid', uid)
}

function uid() {
  return wx.getStorageSync('uid')
}

function setUnionid(unionid) {
  wx.setStorageSync('unionid', unionid)
}

function unionid() {
  return wx.getStorageSync('unionid')
}

function setOpenid(openid) {
  wx.setStorageSync('openid', openid)
}

function openid() {
  return wx.getStorageSync('openid')
}

function setPhone(phone) {
  wx.setStorageSync('phone', phone)
}

function phone() {
  return wx.getStorageSync('phone')
}

function accessToken() {
  return wx.getStorageSync('accessToken')
}

function setAccessToken(accessToken) {
  wx.setStorageSync('accessToken', accessToken)
}

function setSessionID(sessionID) {
  wx.setStorageSync('sessionID', sessionID)
}

function sessionID() {
  return wx.getStorageSync('sessionID')
}

function setUserInfo(userInfo) {
  wx.setStorageSync('userInfo', userInfo)
}

function userInfo() {
  return wx.getStorageSync('userInfo')
}


function setIsFirstLaunch(isFirstLaunch) {
  wx.setStorageSync('isFirstLaunch', isFirstLaunch)
}

function getIsFirstLaunch() {
  var isFirstLaunch = wx.getStorageSync('isFirstLaunch')
  if (isFirstLaunch === false){
    return false
  }
  return  true
}

function isLogin() {
  if (sessionID()) {
    return true
  }
  return false
}

function clean() {
  setPhone(null)
  setSessionID(null)
  setUserInfo(null)
}

function locSystemInfo() {
  try {
    var res = wx.getSystemInfoSync()
    console.log('获取系统信息：' + res)
    let model = SystemModel.initSytemModel(res)
    wx.setStorage({
      key: 'SystemModelInfo',
      data: model,
    })
    return model
  } catch (e) {
    // Do something when catch error
  }
}

function setWifiInfo(ssid, password) {
  wx.setStorageSync(ssid, password)
}

function wifiPassword(ssid) {
  return wx.getStorageSync(ssid)
}

function setSex(sex) {
  wx.setStorageSync('sex', sex)
}

function sex() {
  return wx.getStorageSync('sex')
}

function setDate(date) {
  wx.setStorageSync('date', date)
}

function date() {
  return wx.getStorageSync('date')
}

function setCurrentCar(data) {
  wx.setStorageSync('currentCar', data)
}

function currentCar() {
  return wx.getStorageSync('currentCar')
}

module.exports = {
  setValue: setValue,
  value: value,
  setUid: setUid,
  uid: uid,
  setPhone: setPhone,
  phone: phone,
  accessToken: accessToken,
  setAccessToken: setAccessToken,
  setSessionID: setSessionID,
  sessionID: sessionID,
  setUserInfo: setUserInfo,
  userInfo: userInfo,
  isLogin: isLogin,
  clean: clean,
  locSystemInfo: locSystemInfo,
  unionid: unionid,
  setUnionid: setUnionid,
  openid: openid,
  setOpenid: setOpenid,
  setIsFirstLaunch: setIsFirstLaunch,
  getIsFirstLaunch: getIsFirstLaunch,
  setCurrentCar: setCurrentCar,
  currentCar: currentCar,
}